package com.qys.livesMall.product.controller.sys;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.product.entity.ProductDistributor;
import com.qys.livesMall.product.entity.query.RequestData;
import com.qys.livesMall.product.service.ProductDistributorService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 *  供应商
 *
 * @author
 * @since 2023-06-01
 */
@RestController
@RequestMapping("/productDistributor")
public class ProductDistributorController {
    @Resource
    ProductDistributorService productDistributorService;
    @GetMapping("/{id}")
    public R<ProductDistributor> get(@PathVariable Long id){
        ProductDistributor productDistributor=productDistributorService.getById(id);
        return R.ok(productDistributor);
    }
    @PostMapping("/listPage")
    public R<Page<ProductDistributor>> get(@RequestBody RequestData<ProductDistributor> page){
        ProductDistributor newdistributor = new ProductDistributor();
        BeanUtil.copyProperties(page.getData(), newdistributor, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        LambdaQueryWrapper<ProductDistributor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(newdistributor);
        Page<ProductDistributor> ipage = productDistributorService.page(page.getPage(),queryWrapper);
        return R.ok(ipage);
    }
    @PostMapping("/save")
    public R<Void> save(@RequestBody ProductDistributor distributor){
        String name = SecurityUtils.getUsername();
        ProductDistributor newdistributor = new ProductDistributor();
        BeanUtil.copyProperties(distributor, newdistributor, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Long id = newdistributor.getId();
        if (id != null && id.equals(null)){
            newdistributor.setUpdateTime(LocalDateTime.now());
            newdistributor.setUpdateBy(name);
        }else {
            newdistributor.setCreateTime(LocalDateTime.now());
            newdistributor.setUpdateTime(LocalDateTime.now());
            newdistributor.setCreateBy(name);
            newdistributor.setUpdateBy(name);
        }
        LambdaQueryWrapper<ProductDistributor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(newdistributor);
        boolean isSave = productDistributorService.saveOrUpdate(newdistributor);
        return isSave?R.ok():R.fail();
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id){
        ProductDistributor distributor = productDistributorService.getById(id);
        distributor.setIsDelete(!distributor.getIsDelete());
        boolean f = productDistributorService.updateById(distributor);
        return f?R.ok():R.fail();
    }

}
